package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.train.Station;


import java.util.HashMap;
import java.util.List;


public class StationTableViewController {

    @FXML
    private StackPane stackPane;

    @FXML
    private TableView<Station> tableView;

    @FXML
    private TableColumn<Station, String> stationTableColumn;

    @FXML
    private TableColumn<Station, Integer> routeNumTableColumn;

    @FXML
    private TableColumn<Station, Void> deleteTableColumn;

    @FXML
    private Label label;

    public void initialize(){
        if(ManagerController.type == 0)
            typeZero();
        else
            typeOne();
    }

    //所有站点
    public void typeZero(){
        Station station = new Station();
        HashMap<String, Station> hashMap = station.obtainAllStation();
        ObservableList<Station> observableList = FXCollections.observableArrayList(hashMap.values());

        stationTableColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        routeNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().routeNumProperty().asObject());
        deleteTableColumn.setCellFactory(new Callback<TableColumn<Station, Void>, TableCell<Station, Void>>() {
            @Override
            public TableCell<Station, Void> call(TableColumn<Station, Void> stationVoidTableColumn) {
                return new TableCell<Station, Void>(){
                    private final Button delButton = new Button("删除");

                    {
                        Pop_ups popUps = new Pop_ups();
                        delButton.setOnAction(e -> {
                            Station station = getTableView().getItems().get(getIndex());
                            if(judgeDelStation(station)) {
                                observableList.remove(station);
                                popUps.pop_ups4("删除成功");
                                station.storageStation(observableList);
                            }
                        });
                    }
                    @Override
                    protected void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });
        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Station> c) ->{
            label.setVisible(observableList.isEmpty());
        });
        tableView.setItems(observableList);
    }

    //一个站点
    public void typeOne(){
        Station station = new Station();
        List<Station> list = station.searchStation(ManagerController.name);
        HashMap<String, Station> hashMap = new HashMap<>();
//        hashMap.put(station1.getName(), station1);
        ObservableList<Station> observableList = FXCollections.observableArrayList(list);

        stationTableColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        routeNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().routeNumProperty().asObject());
        deleteTableColumn.setCellFactory(new Callback<TableColumn<Station, Void>, TableCell<Station, Void>>() {
            @Override
            public TableCell<Station, Void> call(TableColumn<Station, Void> stationVoidTableColumn) {
                return new TableCell<Station, Void>(){
                    private final Button delButton = new Button("  删除  ");

                    {
                        Pop_ups popUps = new Pop_ups();
                        delButton.setOnAction(e -> {
                            Station station = getTableView().getItems().get(getIndex());
                            if(judgeDelStation(station)) {
                                observableList.remove(station);
                                popUps.pop_ups4("站点删除成功");
                                station.delStation(station.getName());
                            }
                        });
                    }
                    @Override
                    protected void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });
        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Station> c) ->{
            label.setVisible(observableList.isEmpty());
        });
        tableView.setItems(observableList);
    }
    //判断是否可删除
    public boolean judgeDelStation(Station station){
        if(station.getRouteNum() == 0){
            return true;
        }
        else{
            Pop_ups popUps = new Pop_ups();
            popUps.pop_ups3("该站点存在火车路线,无法删除");
            return false;
        }
    }
}
